const {send} = require("./res")

//判断是否有下一页
const isNextPageFn = (pageIndex,pageSize,total) => ((pageIndex * pageSize) < total )
//计算偏移量
const getOffsetFn = (pageIndex = 1,pageSize = 10) => (pageIndex - 1)*pageSize

async function pageingQuery(selectFn,selectTotalFn,pageIndex = 1,pageSize = 10,ctx){
 try {
  let list = await selectFn(getOffsetFn(pageIndex,pageSize),pageSize);
  let totalSize = await selectTotalFn()
  //查询是否有下一页
  let isNextPage = isNextPageFn(pageIndex,pageSize,totalSize.totalSize);
  send(ctx,200,'200','查询列表成功',list,{...totalSize,isNextPage})
 } catch (error) {
  console.log(error);
 }
}

module.exports = {
  isNextPageFn,
  getOffsetFn,
  pageingQuery
}